#include "UsingDirectives.h"

#include "main2.h"

UsingDirectives::UsingDirectives(SyntaxTreeNode *syntaxNode, SemanticNode *parent) : SemanticNode(parent) {
	for (int i = 0; i < syntaxNode->Nodes->GetCount(); i++) {
		std::string us = flattenNamePeriodAdd(*syntaxNode->Nodes->GetData(i)->Nodes->GetData(1));
		if (syntaxNode->Nodes->GetData(i)->Production == Gr_UsingDirective__using_Name_spSemiCol) {
			usingWithoutAlias.push_back(us);
		} else if (syntaxNode->Nodes->GetData(i)->Production == Gr_UsingDirective__using_Name_as_NAME_spSemiCol) {
			LexicUnit lu = CompilerData->Units->GetLexicUnit(syntaxNode->Nodes->GetData(i)->Nodes->GetData(3)->LexUnitID);
			std::string alias = CompilerData->Units->GetLexicUnitString(&lu);
			if (usingWithAlias.count(alias) > 0) {
				// ToDo: greska
			}
			usingWithAlias[alias] = us;
		}
	}
}

UsingDirectives::~UsingDirectives() {
}



std::string UsingDirectives::toDebugString(int tab) {
	std::ostringstream ss;
	for (int i = 0; i < tab; i++) ss << DEBUG_STRING_RAZMAK;
	ss << "Usings: { ";
	for (std::vector<std::string>::iterator i = usingWithoutAlias.begin(); i != usingWithoutAlias.end(); ++i)
		ss << *i << " ";
	for (std::map<std::string, std::string>::iterator i = usingWithAlias.begin(); i != usingWithAlias.end(); ++i)
		ss << i->second << "-as-" << i->first << " ";
	ss << "}" << std::endl;
	return ss.str();
}

